home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume8 / tabs < prev    next >
Encoding:
Internet Message Format  |  1987-03-01  |  5.7 KB

  1. Subject:  v08i093:  A tab/space conversion program
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: Bob Larson <seismo!usc-oberon!blarson>
  6. Mod.sources: Volume 8, Issue 93
  7. Archive-name: tabs
  8.  
  9. Here's a fairly simple program to convert tab stops, delete trailing
  10. spaces, optimize spaces, etc.  It's probably not to hard to convice one
  11. (or more) standard unix utilities into doing this, but I wrote it for a
  12. non-unix system (Os9/68k).  Probably the least portable thing about it
  13. is it expects input on stdin, and outputs to stdout.  (I've only tested
  14. it on Os9/68k and 4.3 bsd.) Sorry I don't have a man page for it, but I
  15. havn't bothered to learn *roff yet.  The comment block in the begining
  16. contains the nessisary information.
  17.  
  18. [  I wrote the manpage, based on the comments, and the Makefile.   There
  19.    are Unix programs to do this, but not public-domain and this is
  20.    useful.  --r$  ]
  21.  
  22. #! /bin/sh
  23. # This is a shell archive.  Remove anything before this line,
  24. # then unpack it by saving it in a file and typing "sh file".
  25. # If all goes well, you will see the message "End of shell archive."
  26. # Contents:  Makefile tabs.1 tabs.c
  27. PATH=/bin:/usr/bin:/usr/ucb; export PATH
  28. echo shar: extracting "'Makefile'" '(197 characters)'
  29. if test -f 'Makefile' ; then 
  30.   echo shar: will not over-write existing file "'Makefile'"
  31. else
  32. sed 's/^X//' >Makefile <<'@//E*O*F Makefile//'
  33. Xtabs:    tabs.c
  34. X    $(CC) $(CFLAGS) -o tabs tabs.c
  35. X
  36. X# Edit appropriately
  37. XDESTDI    = /usr/local/bin
  38. XMANDIR    = /usr/man/man1
  39. XMANDEST    = tabs.1
  40. Xinstall:    tabs
  41. X    cp tabs $(DESTDIR)
  42. X    cp tabs.1 $(MANDIR)/$(MANDEST)
  43. @//E*O*F Makefile//
  44. if test 197 -ne "`wc -c <'Makefile'`"; then
  45.     echo shar: error transmitting "'Makefile'" '(should have been 197 characters)'
  46. fi
  47. fi # end of overwriting check
  48. echo shar: extracting "'tabs.1'" '(1437 characters)'
  49. if test -f 'tabs.1' ; then 
  50.   echo shar: will not over-write existing file "'tabs.1'"
  51. else
  52. sed 's/^X//' >tabs.1 <<'@//E*O*F tabs.1//'
  53. X.TH TABS 1 LOCAL
  54. X.SH NAME
  55. Xtabs \- canonicalize spaces and tabs
  56. X.SH SYNOPSIS
  57. Xtabs
  58. X[
  59. X.BI \-i NN
  60. X] [
  61. X.BI \-o NN
  62. X] [
  63. X.B \-t
  64. X] <input >output
  65. X.SH DESCRIPTION
  66. XThis program can be used to:
  67. X.RS
  68. X.nf
  69. Xconvert from one tab size to another
  70. Xoptimize spacing/tabbing to a minimum number of characters
  71. Xreplace tabs with spaces
  72. Xreplace spaces with tabs
  73. Xdelete trailing spaces/tabs on lines
  74. X.fi
  75. X.RE
  76. X.PP
  77. XIt is designed to be used as a filter, reading standard input and writing
  78. Xto standard output.
  79. X.PP
  80. X.I Tabs
  81. Xaccepts the following options:
  82. X.IP "\-iNN"
  83. XTabs on the input file are set every NN (default eight) spaces.
  84. XIf NN is 0 or omitted, input tabs are not treated as special characters;
  85. Xthis is probably only useful with the options ``-o0 and -t''.
  86. X.IP "\-oNN"
  87. XTabs on the output file are set every NN (default eight) spaces.
  88. XIf NN is 0 or omitted, tabs are not used in the output file.
  89. X.IP "\-t"
  90. XSuppress trailing spaces/tabs from output lines.
  91. X.PP
  92. XOptions may be specified separately or together.  Spaces are not allowed
  93. Xbetween the i or o option and the number following.
  94. XThis program will always eliminate redundant spacing.  If this is all
  95. Xthat is desired, set the input and output tab size identically.
  96. X.SH "EXAMPLES"
  97. X    tabs -i8 -o8 <infile >outfile
  98. X.br
  99. XOptimize out extra spaces, normal tab stops.
  100. X.PP
  101. X    tabs -i4t <infile >outfile
  102. X.br
  103. XConvert from Microware's silly four-character tabs to normal
  104. Xeight-character tabs and eliminate trailing spaces.
  105. @//E*O*F tabs.1//
  106. if test 1437 -ne "`wc -c <'tabs.1'`"; then
  107.     echo shar: error transmitting "'tabs.1'" '(should have been 1437 characters)'
  108. fi
  109. fi # end of overwriting check
  110. echo shar: extracting "'tabs.c'" '(1711 characters)'
  111. if test -f 'tabs.c' ; then 
  112.   echo shar: will not over-write existing file "'tabs.c'"
  113. else
  114. sed 's/^X//' >tabs.c <<'@//E*O*F tabs.c//'
  115. X/* tabs.c by Robert A. Larson
  116. X */
  117. X
  118. X#include <stdio.h>
  119. X#define inchar()    getc(stdin)
  120. X#define outchar(c)    putc(c, stdout)
  121. X#define TRUE    1
  122. X#define FALSE    0
  123. X
  124. Xstatic unsigned otab = 8;
  125. Xstatic unsigned lpos = 0;
  126. Xstatic unsigned nsp  = 0;
  127. X
  128. Xoutspace() {
  129. X  register int i;
  130. X  if(otab) {
  131. X    while(nsp>1 && nsp >= (i=otab-(lpos%otab))) {
  132. X      outchar('\t');
  133. X      nsp -= i;
  134. X      lpos += i;
  135. X    }
  136. X  }
  137. X  lpos += nsp;
  138. X  while(nsp--) outchar(' ');
  139. X  nsp = 0;
  140. X}
  141. X
  142. Xmain(argc,argv)
  143. Xint argc;
  144. Xregister char **argv;
  145. X{
  146. X  register int c;
  147. X  register unsigned itab = 8;
  148. X  register int trailsup     = FALSE;
  149. X
  150. X  while(--argc) {
  151. X    switch(**++argv) {
  152. X      case '-':
  153. X    for(;;) {
  154. X      switch(*++*argv) {
  155. X        case '\0': goto nextarg;
  156. X        case 'i':
  157. X          itab = 0;
  158. X          while((c = *++*argv)>='0' && c<='9') {
  159. X        itab *= 10;
  160. X        itab += c - '0';
  161. X          }
  162. X          (*argv)--;
  163. X          break;
  164. X        case 'o':
  165. X          otab = 0;
  166. X          while((c = *++*argv)>='0' && c<='9') {
  167. X        otab *= 10;
  168. X        otab += c - '0';
  169. X          }
  170. X          (*argv)--;
  171. X          break;
  172. X        case 't':
  173. X          trailsup = TRUE;
  174. X          break;
  175. X        default:
  176. X          fprintf(stderr, "Unknown switch: %c\n", **argv);
  177. X          exit(1);
  178. X      }
  179. X    }
  180. Xnextarg: break;
  181. X      default:
  182. X    fprintf(stderr, "Illegal agument: %s\n", *argv);
  183. X    exit(1);
  184. X    }
  185. X  }
  186. X  for(;;){
  187. X    switch(c = inchar()){
  188. X      case -1:
  189. X    if(nsp && !trailsup) outspace();  /* in case the file ends in space */
  190. X    exit(0);
  191. X      case ' ':
  192. X    nsp++;
  193. X    break;
  194. X      case '\t':
  195. X    if(itab) nsp += itab - ((lpos+nsp)%itab);
  196. X    else {
  197. X      if(nsp) outspace();
  198. X      outchar(c);
  199. X      lpos++;
  200. X    }
  201. X    break;
  202. X      case '\n':
  203. X    if(nsp && !trailsup) outspace();
  204. X    outchar(c);
  205. X    lpos = nsp = 0;
  206. X    break;
  207. X      default:
  208. X    if(nsp) outspace();
  209. X    outchar(c);
  210. X    lpos++;
  211. X    break;
  212. X    }
  213. X  }
  214. X}
  215. @//E*O*F tabs.c//
  216. if test 1711 -ne "`wc -c <'tabs.c'`"; then
  217.     echo shar: error transmitting "'tabs.c'" '(should have been 1711 characters)'
  218. fi
  219. fi # end of overwriting check
  220. echo shar: "End of shell archive."
  221. exit 0
  222.